<?php
$haveResearchPublication = count($researchPublicationForm->employeeResearchPublications) > 0;
?>

<a name="researchPublication"></a>
<!-- this is research publication section -->
<?php if ($employeeResearchPublicationPermissions->canCreate() || ($haveResearchPublication && $employeeResearchPublicationPermissions->canUpdate())) { ?>
    <div id="changeResearchPublication">
        <div class="head">
            <h1 id="headChangeResearchPublication"><?php echo __('Add Research Publication'); ?></h1>
        </div>

        <div class="inner">
            <form id="frmResearchPublication"
                  action="<?php echo url_for('pim/saveDeleteResearchPublication?empNumber=' .
                      $empNumber . "&option=save"); ?>" method="post">
                <?php echo $researchPublicationForm['_csrf_token']; ?>
                <?php echo $researchPublicationForm['emp_number']->render(); ?>
                <?php echo $researchPublicationForm["seqno"]->render(); ?>

                <fieldset>
                    <ol>
                        <li>
                            <?php echo $researchPublicationForm['title']->renderLabel(__('Research Publication Title') . ' <em>*</em>'); ?>
                            <?php echo $researchPublicationForm['title']->render(array("class" => "formInputText", "maxlength" => 255)); ?>
                        </li>
                        <li>
                            <?php echo $researchPublicationForm['authors']->renderLabel(__('Research Publication Authors') . ' <em>*</em>'); ?>
                            <?php echo $researchPublicationForm['authors']->render(array("class" => "formInputText", "maxlength" => 150)); ?>
                        </li>
                        <li>
                            <?php echo $researchPublicationForm['publisher']->renderLabel(__('Research Publication\'s Publisher' . ' <em>*</em>')); ?>
                            <?php echo $researchPublicationForm['publisher']->render(array("class" => "formInputText", "maxlength" => 150)); ?>
                        </li>
                        <li>
                            <?php echo $researchPublicationForm['publishing_year']->renderLabel(__('Research Publication\'s Publishing Year')); ?>
                            <?php echo $researchPublicationForm['publishing_year']->render(array("class" => "formInputText", "maxlength" => 4)); ?>
                        </li>
                        <li class="required">
                            <em>*</em> <?php echo __(CommonMessages::REQUIRED_FIELD); ?>
                        </li>
                    </ol>
                    <p>
                        <input type="button" class="" id="btnResearchPublicationSave"
                               value="<?php echo __("Save"); ?>"/>
                        <?php if ((!$haveResearchPublication) ||
                            ($haveResearchPublication && $employeeResearchPublicationPermissions->canCreate()) ||
                            ($haveResearchPublication && $employeeResearchPublicationPermissions->canUpdate())
                        ) {
                            ?>
                            <input type="button" class="reset" id="btnResearchPublicationCancel"
                                   value="<?php echo __("Cancel"); ?>"/>
                        <?php } ?>
                    </p>
                </fieldset>
            </form>
        </div>
    </div> <!-- changeResearchPublication  -->
<?php } ?>

<div class="miniList" id="sectionResearchPublication">

    <div class="head">
        <h1><?php echo __("Research Publication"); ?></h1>
    </div>

    <div class="inner">

        <?php if ($employeeResearchPublicationPermissions->canRead()) : ?>

            <?php include_partial('global/flash_messages', array('prefix' => 'researchPublication')); ?>

            <form id="frmDelResearchPublication"
                  action="<?php echo url_for('pim/saveDeleteResearchPublication?empNumber=' .
                      $empNumber . "&option=delete"); ?>" method="post">
                <p id="actionResearchPublication">
                    <?php if ($employeeResearchPublicationPermissions->canCreate()) { ?>
                        <input type="button" value="<?php echo __("Add"); ?>" class="" id="addResearchPublication"/>
                    <?php } ?>
                    <?php if ($employeeResearchPublicationPermissions->canDelete()) { ?>
                        <input type="button" value="<?php echo __("Delete"); ?>" class="delete"
                               id="delResearchPublication"/>
                    <?php } ?>
                </p>
                <table id="" class="table hover">
                    <thead>
                    <tr>
                        <?php if ($employeeResearchPublicationPermissions->canDelete()) { ?>
                            <th class="check" style="width:2%"><input type="checkbox" id="researchPublicationCheckAll"/>
                            </th>
                        <?php } ?>
                        <th><?php echo __('Research Publication Title'); ?></th>
                        <th><?php echo __('Research Publication Authors'); ?></th>
                        <th><?php echo __('Research Publication\'s Publisher'); ?></th>
                        <th><?php echo __('Research Publication\'s Publishing Year'); ?></th>
                    </tr>
                    </thead>
                    <tbody>
                    <?php if (!$haveResearchPublication) { ?>
                        <tr>
                            <?php if ($employeeResearchPublicationPermissions->canDelete()) { ?>
                                <td class="check"></td>
                            <?php } ?>
                            <td><?php echo __(TopLevelMessages::NO_RECORDS_FOUND); ?></td>
                            <td></td>
                            <td></td>
                            <td></td>
                        </tr>
                    <?php } else { ?>
                        <?php
                        $researchPublications = $researchPublicationForm->employeeResearchPublications;
                        $row = 0;
                        foreach ($researchPublications as $researchPublication) :
                            $cssClass = ($row % 2) ? 'even' : 'odd';
                            ?>
                            <tr class="<?php echo $cssClass; ?>">
                                <td class="check">
                                    <input type="hidden" id="title_<?php echo $researchPublication->seqno; ?>"
                                           value="<?php echo htmlspecialchars($researchPublication->title); ?>"/>
                                    <input type="hidden" id="authors_<?php echo $researchPublication->seqno; ?>"
                                           value="<?php echo htmlspecialchars($researchPublication->authors); ?>"/>
                                    <input type="hidden" id="publisher_<?php echo $researchPublication->seqno; ?>"
                                           value="<?php echo htmlspecialchars($researchPublication->publisher); ?>"/>
                                    <input type="hidden" id="publishingYear_<?php echo $researchPublication->seqno; ?>"
                                           value="<?php echo htmlspecialchars($researchPublication->publishing_year); ?>"/>
                                    <?php if ($employeeResearchPublicationPermissions->canDelete()) { ?>
                                        <input type="checkbox" class="chkbox1"
                                               value="<?php echo $researchPublication->seqno; ?>"
                                               name="delResearchPublication[]"/>
                                    <?php } else { ?>
                                        <input type="hidden" class="chkbox1"
                                               value="<?php echo $researchPublication->seqno; ?>"
                                               name="delResearchPublication[]"/>
                                    <?php } ?>
                                </td>
                                <td class="title">
                                    <?php if ($employeeResearchPublicationPermissions->canUpdate()) { ?>
                                        <a class="edit"
                                           href="#"><?php echo htmlspecialchars($researchPublication->title); ?></a>
                                    <?php
                                    } else {
                                        echo htmlspecialchars($researchPublication->title);
                                    }
                                    ?>
                                </td>
                                <td><?php echo htmlspecialchars($researchPublication->authors); ?></td>
                                <td><?php echo htmlspecialchars($researchPublication->publisher); ?></td>
                                <td><?php echo htmlspecialchars($researchPublication->publishing_year); ?></td>
                            </tr>
                            <?php $row++;
                        endforeach;
                    } ?>
                    </tbody>
                </table>
            </form>

        <?php else : ?>
            <div><?php echo __(CommonMessages::DONT_HAVE_ACCESS); ?></div>
        <?php endif; ?>

    </div>

</div> <!-- miniList-sectionResearchPublication -->

<script type="text/javascript">
    //<![CDATA[

    var lang_addResearchPublication = "<?php echo __('Add Research Publication'); ?>";
    var lang_editResearchPublication = "<?php echo __('Edit Research Publication'); ?>";
    var lang_titleRequired = "<?php echo __(ValidationMessages::REQUIRED); ?>";
    var lang_authorsRequired = "<?php echo __(ValidationMessages::REQUIRED); ?>";
    var lang_publisherRequired = "<?php echo __(ValidationMessages::REQUIRED); ?>";
    var lang_exceed255Chars = "<?php echo __(ValidationMessages::TEXT_LENGTH_EXCEEDS, array('%amount%' => 255)); ?>";
    var lang_exceed150Chars = "<?php echo __(ValidationMessages::TEXT_LENGTH_EXCEEDS, array('%amount%' => 150)); ?>";
    var lang_exceed4Chars = "<?php echo __(ValidationMessages::TEXT_LENGTH_EXCEEDS, array('%amount%' => 4)); ?>";
    var lang_selectResearchPublicationToDelete = "<?php echo __(TopLevelMessages::SELECT_RECORDS); ?>";
    var canEdit = '<?php echo $employeeResearchPublicationPermissions->canUpdate(); ?>';

    //]]>
</script>

<script type="text/javascript">
    //<![CDATA[

    $(document).ready(function () {
        //--this section is for research publication

        function addEditLinks() {
            // called here to avoid double adding links - When in edit mode and cancel is pressed.
            removeEditLinks();
            $('form#frmDelResearchPublication table tbody td.name').wrapInner('<a class="edit" href="#"/>');
        }

        function removeEditLinks() {
            $('form#frmDelResearchPublication table tbody td.name a').each(function (index) {
                $(this).parent().text($(this).text());
            });
        }

        //hide add research publication section
        $("#changeResearchPublication").hide();
        $("#researchPublicationRequiredNote").hide();

        //hiding the data table if records are not available
        if ($(".chkbox1").length == 0) {
            $('div#sectionResearchPublication .check').hide();
            $("#editResearchPublication").hide();
            $("#delResearchPublication").hide();
        }

        //if check all button clicked
        $("#researchPublicationCheckAll").click(function () {
            $(".chkbox1").removeAttr("checked");
            if ($("#researchPublicationCheckAll").attr("checked")) {
                $(".chkbox1").attr("checked", "checked");
            }
        });

        //remove tick from the all button if any checkbox unchecked
        $(".chkbox1").click(function () {
            $("#researchPublicationCheckAll").removeAttr('checked');
            if ($(".chkbox1").length == $(".chkbox1:checked").length) {
                $("#researchPublicationCheckAll").attr('checked', 'checked');
            }
        });

        $("#addResearchPublication").click(function () {

            removeEditLinks();
            clearMessageBar();

            //changing the headings
            $("#headChangeResearchPublication").text(lang_addResearchPublication);
            $(".chkbox1").hide();
            $("#researchPublicationCheckAll").hide();

            //hiding action button section
            $("#actionResearchPublication").hide();

            $('div#changeResearchPublication label.error').hide();

            $("#employeeResearchPublication_seqno").val("");
            $("#employeeResearchPublication_title").val("");
            $("#employeeResearchPublication_authors").val("");
            $("#employeeResearchPublication_publisher").val("");
            $("#employeeResearchPublication_publishing_year").val("");

            //show add employeeResearchPublication form
            $("#changeResearchPublication").show();
            $("#researchPublicationRequiredNote").show();
        });

        //clicking of delete button
        $("#delResearchPublication").click(function () {

            clearMessageBar();

            if ($(".chkbox1:checked").length > 0) {
                $("#frmDelResearchPublication").submit();
            } else {
                $("#researchPublicationMessagebar").attr('class', 'messageBalloon_notice').text(lang_selectResearchPublicationToDelete);
            }

        });

        $("#btnResearchPublicationSave").click(function () {
            clearMessageBar();
            $("#frmResearchPublication").submit();
        });

        //form validation
        var researchPublicationValidator =
            $("#frmResearchPublication").validate({
                rules: {
                    'employeeResearchPublication[title]': {
                        required: true,
                        maxlength: 255
                    },
                    'employeeResearchPublication[authors]': {
                        required: true,
                        maxlength: 150
                    },
                    'employeeResearchPublication[publisher]': {
                        required: true,
                        maxlength: 150
                    },
                    'employeeResearchPublication[publishing_year]': {
                        maxlength: 4
                    }
                },
                messages: {
                    'employeeResearchPublication[title]': {
                        required: lang_titleRequired,
                        maxlength: lang_exceed255Chars
                    },
                    'employeeResearchPublication[authors]': {
                        required: lang_authorsRequired,
                        maxlength: lang_exceed150Chars
                    },
                    'employeeResearchPublication[publisher]': {
                        required: lang_publisherRequired,
                        maxlength: lang_exceed150Chars
                    },
                    'employeeResearchPublication[publishing_year]': {
                        maxlength: lang_exceed4Chars
                    }
                }
            });

        $("#btnResearchPublicationCancel").click(function () {
            clearMessageBar();
            if (canEdit) {
                addEditLinks();
            }

            researchPublicationValidator.resetForm();

            $('div#changeResearchPublication label.error').hide();

            $(".chkbox1").removeAttr("checked");

            //hiding action button section
            $("#actionResearchPublication").show();

            $("#changeResearchPublication").hide();
            $("#researchPublicationRequiredNote").hide();

            $(".chkbox1").show();
            $("#researchPublicationCheckAll").show();
        });

        $('form#frmDelResearchPublication table a.edit').live('click', function (event) {
            event.preventDefault();

            var seqno = $(this).closest("tr").find('input.chkbox1:first').val();
            clearMessageBar();

            //changing the headings
            $("#headChangeResearchPublication").text(lang_editResearchPublication);

            $('div#changeResearchPublication label.error').hide();

            //hiding action button section
            $("#actionResearchPublication").hide();

            //show add  employeeResearchPublication form
            $("#changeResearchPublication").show();

            $("#employeeResearchPublication_seqno").val(seqno);
            $("#employeeResearchPublication_title").val($("#title_" + seqno).val());
            $("#employeeResearchPublication_authors").val($("#authors_" + seqno).val());
            $("#employeeResearchPublication_publisher").val($("#publisher_" + seqno).val());
            $("#employeeResearchPublication_publishing_year").val($("#publishingYear_" + seqno).val());

            $("#researchPublicationRequiredNote").show();

            $(".chkbox1").hide();
            $("#researchPublicationCheckAll").hide();
        });

    });

    function fillDataToResearchPublicationDataPane(seqno) {
        return false;
    }
    //]]>
</script>